/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package resources;

import beans.UserEntity;
import beans.UserEntityFacade;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;

/**
 * REST Web Service
 *
 * @author ajou
 */
@Path("user")
@RequestScoped
public class UserResource {
    UserEntityFacade uef = lookupUserEntityFacadeBean();

//    @Context
//    private UriInfo context;
//
    /**
     * Creates a new instance of UserResource
     */
    public UserResource() {
    }

    /**
     * Retrieves representation of an instance of resources.UserResource
     * @return an instance of java.lang.String
     */
    @GET
    @Produces("application/json")
    public List<UserEntity> getJson() {
        
        for (UserEntity users : uef.findAll()) {
            System.out.println(users.getName());
        }
        return uef.findAll();
    }
    
    @POST
    @Produces("application/json")
    @Consumes({"application/json", "application/x-www-form-urlencoded"})
    public String doPost(@FormParam("name") String name, @FormParam("pwd") String pwd, @FormParam("team") String team) {
        System.out.println("name: " + name.toString());
        System.out.println("pwd: " + pwd.toString());
        System.out.println("team: " + team.toString());
        for (UserEntity users : uef.findAll()) {
            if (users.getName().equalsIgnoreCase(name.toString()) && users.getPwd().equalsIgnoreCase(pwd.toString()))
                return "LOGIN_SUCCESS";
        }
        return "LOGIN_FAILED";
    }    

    /**
     * PUT method for updating or creating an instance of UserResource
     * @param content representation for the resource
     * @return an HTTP response with content of the updated or created resource.
     */
    @PUT
    @Consumes("application/json")
    public void putJson(String content) {
    }

    private UserEntityFacade lookupUserEntityFacadeBean() {
        try {
            javax.naming.Context c = new InitialContext();
            return (UserEntityFacade) c.lookup("java:global/ACserver/ACserver-ejb/UserEntityFacade!beans.UserEntityFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
